package sanshui.system.trade.service;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.db.entity.TradeAccount;
import sanshui.system.trade.db.entity.TradeWorker;
import sanshui.system.trade.db.entity.TradeWorkerExample;
import sanshui.system.trade.db.mapper.TradeAccountMapper;
import sanshui.system.trade.db.mapper.TradeWorkerMapper;
import sanshui.system.trade.factory.data.FilterModel;
import sanshui.system.trade.factory.data.TradeSystemType;
import sanshui.system.trade.factory.trade.worker.AbstractTradeWorker;
import sanshui.system.trade.model.AccLoginInfoModel;
import sanshui.system.trade.model.TradeReportModel;
import sanshui.system.trade.model.TradeWorkerModel;
import sanshui.system.trade.util.CommonTimeUtil;
import sanshui.system.trade.util.IdWorkerUtil;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class WorkerServiceImpl implements IWorkerService {

    @Resource
    IdWorkerUtil idWorkerUtil;
    @Resource
    TradeWorkerMapper tradeWorkerMapper;
    @Resource
    TradeAccountMapper accountMapper;
    @Resource
    ITradeTargetCodeService tradeTargetCodeService;

    @Override
    public String insertBackTradeWorkerIfDefault(TradeWorkerModel tradeWorkerModel) {
        Long userId = tradeWorkerModel.getUserId();
        if (Strings.isEmpty(tradeWorkerModel.getEndTime())){
            tradeWorkerModel.setEndTime(CommonTimeUtil.getNowTime16());
        }

        Long accId;
        if (null == tradeWorkerModel.getAccId()){
            TradeAccount tradeAccount = new TradeAccount();
            tradeAccount.setUserId(userId);
            tradeAccount.setAccDesc("");
            tradeAccount.setInitValue(Long.parseLong(tradeWorkerModel.getInitValue()) * 100L);
            tradeAccount.setCurrentCash(Long.parseLong(tradeWorkerModel.getInitValue()) * 100L);
            tradeAccount.setCurrentValue(0L);
            tradeAccount.setAccStatus(TradeType.AccountStatus.INIT.getIndex());
            tradeAccount.setDelFlg(TradeType.DelFlg.EXIT.getCode());
            tradeAccount.setStartDatetime(tradeWorkerModel.getStartTime());
            tradeAccount.setEndDatetime(tradeWorkerModel.getEndTime());
            tradeAccount.setMarketType(tradeWorkerModel.getTradeMarketKey());
            tradeAccount.setFeeRateAround(Integer.parseInt(tradeWorkerModel.getFeeRateAround()));
            tradeAccount.setBuyFeeRateGuo(Integer.parseInt(tradeWorkerModel.getBuyFeeRateGuo()));
            tradeAccount.setBuyFeeRateQuan(Integer.parseInt(tradeWorkerModel.getBuyFeeRateQuan()));
            tradeAccount.setSellFeeRateGuo(Integer.parseInt(tradeWorkerModel.getSellFeeRateGuo()));
            tradeAccount.setSellFeeRateQuan(Integer.parseInt(tradeWorkerModel.getSellFeeRateQuan()));
            tradeAccount.setSellFeeRateYin(Integer.parseInt(tradeWorkerModel.getSellFeeRateYin()));
            tradeAccount.setSellFixedFee(Integer.parseInt(tradeWorkerModel.getSellFixedFee()));
            tradeAccount.setBuyFixedFee(Integer.parseInt(tradeWorkerModel.getBuyFixedFee()));
            tradeAccount.setFeeAround(100);
            tradeAccount.setAccountType(tradeWorkerModel.getTradeSystemType());
            tradeAccount.setAccId(idWorkerUtil.nowId(IdWorkerUtil.IdType.ACCOUNT_ID));
            accountMapper.insert(tradeAccount);
            accId = tradeAccount.getAccId();
            log.info("创建的accId：{}", accId);
            tradeWorkerModel.setAccId(accId);
        }
        accId = tradeWorkerModel.getAccId();

        TradeWorker tradeWorker = new TradeWorker();
        TradeAccount tradeAccount = accountMapper.selectByPrimaryKey(accId);
        tradeWorker.setUserId(userId);
        tradeWorker.setAccId(accId);
        tradeWorker.setTradeMarketKey(tradeAccount.getMarketType());
        if (tradeWorkerModel.getWorkerId() != null){
            TradeWorker oldWorker = tradeWorkerMapper.selectByPrimaryKey(tradeWorkerModel.getWorkerId());
            tradeWorker.setTradeFrequent(oldWorker.getTradeFrequent());
            tradeWorker.setTradeSystemType(tradeAccount.getAccountType());
            tradeWorker.setDataMarketType(oldWorker.getDataMarketType());
            tradeWorker.setFilterRule(JSONObject.toJSONString(oldWorker.getFilterModel()));
            tradeWorker.setWorkerDesc(oldWorker.getWorkerDesc());
            tradeWorker.setIndicatorCodes(oldWorker.getIndicatorCodes());
            tradeWorker.setStrategyCode(oldWorker.getStrategyCode());
            tradeWorker.setRiskCtr(oldWorker.getRiskCtr());
            tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
            tradeWorker.setWorkerStatus(TradeType.WorkerStatus.INIT.getCode());
            tradeWorker.setDelFlg(TradeType.DelFlg.EXIT.getCode());
            tradeWorker.setStartTime(oldWorker.getStartTime());
            tradeWorker.setEndTime(oldWorker.getEndTime());
            tradeWorker.setCreateTime(CommonTimeUtil.getNowTime16());
            tradeWorker.setLastStartUpTime(CommonTimeUtil.getNowTime16());
            tradeWorker.setStrategyParam(tradeWorkerModel.getStrategyParam());
            tradeWorkerMapper.insert(tradeWorker);
            log.info("创建的worker:{}", JSONObject.toJSONString(tradeWorker));
            List<String> chooseTargets = tradeWorkerModel.getChooseTargets();
            tradeTargetCodeService.insert(chooseTargets,  tradeWorkerModel.getWorkerId());
            return idWorkerUtil.nowId(IdWorkerUtil.IdType.WORKER_ID).toString();
        }
        tradeWorker.setTradeFrequent(tradeWorkerModel.getFrequency());
        tradeWorker.setTradeSystemType(tradeWorkerModel.getTradeSystemType());
        tradeWorker.setDataMarketType(tradeWorkerModel.getDataMarketType());
        tradeWorker.setFilterRule(JSONObject.toJSONString(tradeWorkerModel.getFilterModel()));

        tradeWorker.setWorkerDesc(tradeWorkerModel.getTradeName());
        tradeWorker.setIndicatorCodes(Strings.join(tradeWorkerModel.getIndicators(), ','));
        tradeWorker.setTargetIndex(Strings.join(tradeWorkerModel.getChooseTargetsIndex(), ','));
        tradeWorker.setTargetCodes(Strings.join(tradeWorkerModel.getChooseTargets(), ','));
        tradeWorker.setStrategyCode(tradeWorkerModel.getStrategy());
        tradeWorker.setRiskCtr(tradeWorkerModel.getRiskStrategy());
        tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setWorkerStatus(TradeType.WorkerStatus.INIT.getCode());
        tradeWorker.setDelFlg(TradeType.DelFlg.EXIT.getCode());
        tradeWorker.setStartTime(tradeAccount.getStartDatetime());
        tradeWorker.setEndTime(tradeAccount.getEndDatetime());
        tradeWorker.setCreateTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setLastStartUpTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setWorkerId(idWorkerUtil.nowId(IdWorkerUtil.IdType.WORKER_ID));
        tradeWorker.setStrategyParam(tradeWorkerModel.getStrategyParam());
        tradeWorkerMapper.insert(tradeWorker);
        List<String> chooseTargets = tradeWorkerModel.getChooseTargets();
        tradeTargetCodeService.insert(chooseTargets,  tradeWorker.getWorkerId());
        log.info("创建的worker:{}", JSONObject.toJSONString(tradeWorker));
        return accId.toString();
    }

    /**
     * 回测系统创建虚拟账户进行回测
     * */
    @Override
    public boolean buildBackTradeWorker(TradeWorkerModel tradeWorkerModel) {
        // 创建虚拟账户用于回测
        TradeAccount tradeAccount = this.buildAccByTradeModel(tradeWorkerModel, TradeType.AccountType.VIRTUAL);
        tradeWorkerModel.setAccId(tradeAccount.getAccId());
        TradeWorker tradeWorker = this.builWorkerByTradeModel(tradeWorkerModel, TradeType.WorkerStatus.INIT);
        return null != tradeAccount && null != tradeWorker;
    }

    @Override
    public TradeWorkerModel buildWorkerModel(Long workerId) {
        return selectWorkerModelById(workerId);
    }

    @Override
    public TradeReportModel selectWorkerReportById(Long workerId) {
        return null;
    }

    @Override
    public List<TradeWorkerModel> selectWorkerList(TradeWorkerModel workerModel) {

        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        if (Strings.isNotEmpty(workerModel.getWorkerStatus())){
            criteria.andWorkerStatusEqualTo(workerModel.getWorkerStatus());
        }
        if (Strings.isNotEmpty(workerModel.getTradeSystemType())){
            criteria.andTradeSystemTypeEqualTo(workerModel.getTradeSystemType());
        }
        if (Strings.isNotEmpty(workerModel.getCreateTime())) {
            criteria.andCreateTimeGreaterThan(workerModel.getCreateTime());
        }
        if (workerModel.getAccId() != null && workerModel.getAccId() > 0L) {
            criteria.andAccIdEqualTo(workerModel.getAccId());
        }

        tradeWorkerExample.setOrderByClause("create_time DESC");

        PageHelper.startPage(workerModel.getPageNum(), workerModel.getPageSize());
        List<TradeWorker> tradeWorkers = tradeWorkerMapper.selectByExample(tradeWorkerExample);
        return BeanUtil.copyToList(tradeWorkers, TradeWorkerModel.class);
    }

    @Override
    public TradeWorker selectWorkerById(Long workerId) {
        return tradeWorkerMapper.selectByPrimaryKey(workerId);
    }

    @Override
    public List<TradeWorker> selectWorkerByAccId(Long accId) {
        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        if (null != accId){
            criteria.andAccIdEqualTo(accId);
        }
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        return tradeWorkerMapper.selectByExample(tradeWorkerExample);
    }

    @Override
    public TradeWorkerModel selectWorkerModelById(Long workerId) {
        TradeWorker worker = tradeWorkerMapper.selectByPrimaryKey(workerId);
        TradeWorkerModel tradeWorkerModel = new TradeWorkerModel();
        tradeWorkerModel.setUserId(worker.getUserId());
        tradeWorkerModel.setAccId(worker.getAccId());
        tradeWorkerModel.setWorkerId(worker.getWorkerId());
        tradeWorkerModel.setFrequency(worker.getTradeFrequent());
        tradeWorkerModel.setFilterModel(worker.getFilterModel());
        tradeWorkerModel.setStrategy(worker.getStrategyCode());
        tradeWorkerModel.setRiskStrategy(worker.getRiskCtr());
        tradeWorkerModel.setTradeMarketKey(worker.getTradeMarketKey());
        tradeWorkerModel.setTradeSystemType(worker.getTradeSystemType());
        tradeWorkerModel.setDataMarketType(worker.getDataMarketType());
        tradeWorkerModel.setChooseTargets(this.splite(worker.getTargetCodes(), ","));
        tradeWorkerModel.setChooseTargetsIndex(this.splite(worker.getTargetIndex(), ","));
        tradeWorkerModel.setIndicators(this.splite(worker.getIndicatorCodes(), ","));
        tradeWorkerModel.setWorkerStatus(worker.getWorkerStatus());
        tradeWorkerModel.setStartTime(worker.getStartTime());
        tradeWorkerModel.setEndTime(worker.getEndTime());
        tradeWorkerModel.setUpdateTime(worker.getUpdateTime());
        TradeAccount account = accountMapper.selectByPrimaryKey(worker.getAccId());
        tradeWorkerModel.setInitValue(account.getInitValue().toString());
        tradeWorkerModel.setBuyFeeRateGuo(account.getBuyFeeRateGuo().toString());
        tradeWorkerModel.setBuyFixedFee(account.getBuyFixedFee().toString());
        tradeWorkerModel.setBuyFeeRateQuan(account.getBuyFeeRateQuan().toString());
        tradeWorkerModel.setSellFixedFee(account.getSellFixedFee().toString());
        tradeWorkerModel.setSellFeeRateGuo(account.getSellFeeRateGuo().toString());
        tradeWorkerModel.setSellFeeRateYin(account.getSellFeeRateYin().toString());
        tradeWorkerModel.setSellFeeRateQuan(account.getSellFeeRateQuan().toString());
        tradeWorkerModel.setCurrentValue(account.getCurrentValue().toString());
        tradeWorkerModel.setCurrentCash(account.getCurrentCash().toString());

        AccLoginInfoModel accLoginInfoModel = new AccLoginInfoModel();
        accLoginInfoModel.setAccToken(account.getAccToken());
        accLoginInfoModel.setAccUserName(account.getAccUsername());
        accLoginInfoModel.setAccPw(account.getAccPw());
        tradeWorkerModel.setAccLoginInfoModel(accLoginInfoModel);
        return tradeWorkerModel;
    }

    @Override
    public List<TradeWorkerModel> selectWorkers(TradeType.WorkerStatus workerStatus) {
        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        if (null != workerStatus){
            criteria.andWorkerStatusEqualTo(workerStatus.getCode());
        }
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        List<TradeWorker> tradeWorkers = tradeWorkerMapper.selectByExample(tradeWorkerExample);
        List<TradeWorkerModel> models = new ArrayList<>();
        tradeWorkers.forEach(tradeWorker -> models.add(selectWorkerModelById(tradeWorker.getWorkerId())));
        return models;
    }

    @Override
    public List<Long> selectWorkersIdByStatus(TradeType.WorkerStatus workerStatus) {
        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        if (null != workerStatus){
            criteria.andWorkerStatusEqualTo(workerStatus.getCode());
        }
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        List<TradeWorker> tradeWorkers = tradeWorkerMapper.selectByExample(tradeWorkerExample);
        List<Long> models = new ArrayList<>();
        tradeWorkers.forEach(tradeWorker -> models.add(tradeWorker.getWorkerId()));
        return models;
    }

    private List<String> splite(String targets, String regex){
        if (!Strings.isEmpty(targets)){
            if (targets.contains(regex)){
                return Arrays.asList(targets.split(regex));
            }else{
                return Collections.singletonList(targets);
            }
        }
        return new ArrayList<>();
    }

    @Override
    public int updateWorkerStatusByWorkerId(Long workerId, TradeType.WorkerStatus workerStatus, TradeType.WorkerStatus oldStatus) {
        TradeWorker tradeWorker = new TradeWorker();
        tradeWorker.setWorkerId(workerId);
        tradeWorker.setWorkerStatus(workerStatus.getCode());
        tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());

        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        criteria.andWorkerIdEqualTo(workerId);
        if (null != oldStatus){
            criteria.andWorkerStatusEqualTo(oldStatus.getCode());
        }
        return tradeWorkerMapper.updateByExampleSelective(tradeWorker, tradeWorkerExample);
    }

    @Override
    public int updateWorkerByWorkerId(Long workerId, TradeWorker tradeWorker) {
        tradeWorker.setWorkerId(workerId);
        tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
        return tradeWorkerMapper.updateByPrimaryKeySelective(tradeWorker);
    }

    @Override
    public void deleteWorkerById(Long workerId) {
        tradeWorkerMapper.deleteByPrimaryKey(workerId);
    }

    @Override
    public String runRtWorker(String workerId) {
        TradeWorker worker = tradeWorkerMapper.selectByPrimaryKey(Long.valueOf(workerId));

        if (null != worker){
            TradeWorker tradeWorker = new TradeWorker();
            tradeWorker.setWorkerId(worker.getWorkerId());
            tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
            tradeWorker.setWorkerStatus(TradeType.WorkerStatus.INIT.getCode());
            tradeWorker.setLastStartUpTime(CommonTimeUtil.getNowTime16());
            tradeWorkerMapper.updateByPrimaryKeySelective(tradeWorker);
            return workerId;
        }
        return "";
    }


    @Override
    public void clearWorker(String clearTime) {
        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        if (Strings.isNotEmpty(clearTime)){
            criteria.andCreateTimeLessThan(clearTime);
        }
        List<TradeWorker> tradeWorkers = tradeWorkerMapper.selectByExample(tradeWorkerExample);
        for (TradeWorker tradeWorker : tradeWorkers) {
            if (TradeSystemType.BACK_TRADE.name().equalsIgnoreCase(tradeWorker.getTradeSystemType())){
                log.info("定时任务开始删除回测任务创建时间小于:{}, workerId:{}", clearTime, tradeWorker.getWorkerId());
                tradeWorker.setWorkerId(tradeWorker.getWorkerId());
                tradeWorker.setWorkerStatus(TradeType.WorkerStatus.END.getCode());
                tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
                tradeWorker.setDelFlg(TradeType.DelFlg.DELETE.getCode());
                tradeWorkerMapper.updateByPrimaryKeySelective(tradeWorker);
            }

        }
    }

    @Override
    public void checkTaskIfRunInMap(Map<Long, AbstractTradeWorker> workerMap) {
        TradeWorkerExample tradeWorkerExample = new TradeWorkerExample();
        TradeWorkerExample.Criteria criteria = tradeWorkerExample.createCriteria();
        criteria.andDelFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        criteria.andWorkerStatusEqualTo(TradeType.WorkerStatus.IN.getCode());
        List<TradeWorker> tradeWorkers = tradeWorkerMapper.selectByExample(tradeWorkerExample);
        for (TradeWorker tradeWorker : tradeWorkers) {
            if(!workerMap.containsKey(tradeWorker.getWorkerId())) {
                log.info("检查到任务未真实运行, workerId:{}", tradeWorker.getWorkerId());
                tradeWorker.setWorkerId(tradeWorker.getWorkerId());
                tradeWorker.setWorkerStatus(TradeType.WorkerStatus.END.getCode());
                tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
                tradeWorkerMapper.updateByPrimaryKeySelective(tradeWorker);
            }
        }
    }

    public TradeAccount buildAccByTradeModel(TradeWorkerModel tradeWorkerModel, TradeType.AccountType accType){
        TradeAccount tradeAccount = new TradeAccount();
        Long accId = idWorkerUtil.nowId(IdWorkerUtil.IdType.ACCOUNT_ID);
        tradeAccount.setAccId(accId);
        tradeAccount.setUserId(tradeWorkerModel.getUserId());
        tradeAccount.setAccDesc("");
        tradeAccount.setInitValue(Long.parseLong(tradeWorkerModel.getInitValue()) * 100L);
        tradeAccount.setCurrentCash(Long.parseLong(tradeWorkerModel.getInitValue()) * 100L);
        tradeAccount.setCurrentValue(0L);
        tradeAccount.setAccStatus(TradeType.AccountStatus.INIT.getIndex()); // 账户状态，基本上用不到
        tradeAccount.setAccountType(accType.name());
        tradeAccount.setDelFlg(TradeType.DelFlg.EXIT.getCode());
        tradeAccount.setStartDatetime(tradeWorkerModel.getStartTime());
        tradeAccount.setEndDatetime(tradeWorkerModel.getEndTime());
        tradeAccount.setMarketType(tradeWorkerModel.getTradeMarketKey());
        tradeAccount.setFeeRateAround(Integer.parseInt(tradeWorkerModel.getFeeRateAround()));
        tradeAccount.setBuyFeeRateGuo(Integer.parseInt(tradeWorkerModel.getBuyFeeRateGuo()));
        tradeAccount.setBuyFeeRateQuan(Integer.parseInt(tradeWorkerModel.getBuyFeeRateQuan()));
        tradeAccount.setSellFeeRateGuo(Integer.parseInt(tradeWorkerModel.getSellFeeRateGuo()));
        tradeAccount.setSellFeeRateQuan(Integer.parseInt(tradeWorkerModel.getSellFeeRateQuan()));
        tradeAccount.setSellFeeRateYin(Integer.parseInt(tradeWorkerModel.getSellFeeRateYin()));
        tradeAccount.setSellFixedFee(Integer.parseInt(tradeWorkerModel.getSellFixedFee()));
        tradeAccount.setBuyFixedFee(Integer.parseInt(tradeWorkerModel.getBuyFixedFee()));
        tradeAccount.setFeeAround(100);
        tradeAccount.setAccountType(tradeWorkerModel.getTradeSystemType());
        accountMapper.insert(tradeAccount);
        log.info("创建的accId：{}", accId);
        return tradeAccount;
    }

    public TradeWorker builWorkerByTradeModel(TradeWorkerModel tradeWorkerModel, TradeType.WorkerStatus workerStatus){
        TradeWorker tradeWorker = new TradeWorker();
        tradeWorker.setUserId(tradeWorkerModel.getUserId());
        tradeWorker.setAccId(tradeWorkerModel.getAccId());
        tradeWorker.setTradeMarketKey(tradeWorkerModel.getTradeMarketKey());
        tradeWorker.setTradeFrequent(tradeWorkerModel.getFrequency());
        tradeWorker.setTradeSystemType(tradeWorkerModel.getTradeSystemType());
        tradeWorker.setDataMarketType(tradeWorkerModel.getDataMarketType());
        tradeWorker.setFilterRule(JSONObject.toJSONString(tradeWorkerModel.getFilterModel()));

        tradeWorker.setWorkerDesc(tradeWorkerModel.getTradeName());
        tradeWorker.setIndicatorCodes(Strings.join(tradeWorkerModel.getIndicators(), ','));
        tradeWorker.setTargetIndex(Strings.join(tradeWorkerModel.getChooseTargetsIndex(), ','));
        tradeWorker.setTargetCodes(Strings.join(tradeWorkerModel.getChooseTargets(), ','));
        tradeWorker.setStrategyCode(tradeWorkerModel.getStrategy());
        tradeWorker.setRiskCtr(tradeWorkerModel.getRiskStrategy());
        tradeWorker.setUpdateTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setWorkerStatus(workerStatus.name());
        tradeWorker.setDelFlg(TradeType.DelFlg.EXIT.getCode());
        tradeWorker.setStartTime(tradeWorkerModel.getStartTime());
        tradeWorker.setEndTime(tradeWorkerModel.getEndTime());
        tradeWorker.setCreateTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setLastStartUpTime(CommonTimeUtil.getNowTime16());
        tradeWorker.setStrategyParam(tradeWorkerModel.getExtraTradeParams());
        tradeWorkerModel.setWorkerId(tradeWorker.getWorkerId());
        tradeWorkerMapper.insert(tradeWorker);
        log.info("创建的worker:{}", JSONObject.toJSONString(tradeWorker));
        return tradeWorker;
    }
}
